/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-08-13 18:15:54
 * @LastEditors: Clark
 * @LastEditTime: 2023-08-13 18:39:21
 * @Description: file content
 */

#include "s3c2440_soc.h"

// 115200,8n1
void uart0_init()
{
    // 设置引脚用于串口
    // GPH2,3用于TxD0, RxD0
    GPHCON &= ~((3<<4) | (3<<6));
    GPHCON |= ((2<<4) | (2<<6));
    GPHUP &= ~((1<<2) | (1<<3));    // 使能内部上拉

    /**
     * @brief 设置波特率
     * UBRDIVn = (int)UART clock / (buad rate x 16)) - 1
     * UART clock = 50M
     * UBRDIVn = (int)(50000000 / (115200x16)) - 1 = 26
     */
    UCON0 = 0x00000005; // PCLK中断、查询模式
    UBRDIV0 = 26;

    // 设置数据格式
    ULCON0 = 0x00000003;    // 8n1:8数据位，无校验位，1停止位
}

int putchar(int c)
{
    // UTRSTAT0
    // UTXH0
    while (!(UTRSTAT0 & (1<<2)));
    UTXH0 = (unsigned char)c;
}

int getchar(void)
{
    while (!(UTRSTAT0 & (1<<0)));
    return URXH0;
}

int puts(const char *s)
{
    while (*s)
    {
        putchar(*s);
        s++;
    }
}
